﻿using System;
using WebSocketSharp.Server;

namespace Sun.WebSocket.SerialPort.Center
{
  public class WsAccessor
  {
    private WebSocketServer _Server;

    public void Start(int port)
    {
      _Server = new WebSocketServer(port);
      _Server.AddWebSocketService<WsBoxWebSocketBehavior>("/Box");
      _Server.Start();
    }

    public void Stop()
    {
      _Server?.Stop();
    }

    /// <summary>
    /// 向所有已连接至ws的客户端发送数据（文本类型）。
    /// </summary>
    internal void Send(string data)
    {
      _Server.WebSocketServices.Broadcast(data);
    }

    /// <summary>
    /// 向所有已连接至ws的客户端发送数据（二进制类型）。
    /// </summary>
    internal void Send(byte[] rawData)
    {
      _Server.WebSocketServices.Broadcast(rawData);
    }
  }
}